package data;

import java.sql.SQLException;
import java.util.concurrent.ExecutionException;

import android.app.Activity;

/**
 * This abstract class sets up the ability to easily do boolean tasks with the
 * database
 * 
 * @author Pooja
 */
abstract public class DBBooleanTask<T> extends DBTask<T, Void, Boolean> {

	/** Data need to perform the task */
	protected T data;

	/**
	 * Creates a boolean task
	 * 
	 * @param activity
	 *            the activity for the loading bar
	 * @param data
	 *            object needed to perform task
	 * @throws DBInterfaceError
	 *             a possible error
	 */
	public DBBooleanTask(Activity activity, T data) throws DBInterfaceError {
		super(activity);
		this.data = data;
	}

	/**
	 * Calls the actual abstract task method to do the task
	 * 
	 * @param params
	 *            - the list of params for the background method
	 * @returns whether or not the task succeeded
	 */
	@Override
	protected Boolean doInBackground(T... params) {
		return task(params[0]);
	}

	/**
	 * This is where the database code should go
	 * 
	 * @param data
	 *            object need to perform the task
	 * @return true if the task is successful, else false
	 */
	abstract protected boolean task(T data);

	/**
	 * Gets the result of the task
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Boolean getResult() throws DBInterfaceError {

		boolean result;
		execute(data);

		try {
			result = get();
		} catch (InterruptedException e) {
			e.printStackTrace();
			throw new DBInterfaceError(DBInterfaceError.Error.CONNECTION_ERROR);
		} catch (ExecutionException e) {
			e.printStackTrace();
			throw new DBInterfaceError(DBInterfaceError.Error.CONNECTION_ERROR);
		}
		try {
			conn.close();
			System.out.println("fasdfasdf");
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("fasdfasdf");
		}

		return result;
	}

}
